/// Source : https://leetcode.cn/problems/power-of-two/description/
/// Author : bianwangyang
/// Time   : 2024-02-27

#include <iostream>

using namespace std;

/// 231.2的幂
/// Simulation
/// Time Complexity: O(logn)
/// Space Complexity: O(1)
class Solution {
public:
    //  循环除法
    /**
    bool isPowerOfTwo(int n) {
        if(n <= 0) return false;
        while(n % 2 == 0) n /= 2;
        return n == 1;
    }
    */
    // 递归方法
    /**
    bool isPowerOfTwo(int n) {
        if(n == 1) return true; // 2的0次幂
        if(n <= 0 || n % 2 != 0) return false;
        return isPowerOfTwo(n/2);
    }
    */
    // 位运算方法
    bool isPowerOfTwo(int n) {
    //    return n > 0 && (n & -n) == n;  // lowbit 取最低位的1和后面的0
        return n > 0 && (n & (n-1)) == 0;
    }
};


int main() {

    return 0;
}
